import common.ListNode;

import java.util.List;

/**
 * @author tmh
 * @date 2024/10/25 22:36
 * @description
 */
public class T19删除链表的倒数第N个结点 {
    public ListNode removeNthFromEnd(ListNode head, int n) {
        ListNode dummy = new ListNode();
        dummy.next = head;
        ListNode p = head;
        int len = 0;
        while (p != null) {
            p = p.next;
            len++;
        }
        p = dummy;
        for (int i = 0; i < len - n; i++) {
            p = p.next;
        }

        p.next = p.next.next;
        return dummy.next;
    }
}
